//
//  ClubSquareViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/5.
//

import UIKit
import JXSegmentedView
import SnapKit

class ClubSquareViewController: ClubBaseViewController {

    private let searchButton = UIIconButton(style: .leading(itemSpace: 0))
    private var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    private var segmentedDataSource = JXSegmentedTitleDataSource()
    private var listContainerView: JXSegmentedListContainerView!
    private var index = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            searchButton
            segmentedView
        }
        
        loadData()
        
        searchButton.style { v in
            v.iconSize = CGSize(width: 16.0.Scale, height: 16.0.Scale)
            v.setImage("Info_search".toImage, for: .normal)
            v.setTitle("请输入关键进行搜索", for: .normal)
            v.setTitleColor("#AEB0B3".toColor, for: .normal)
            v.contentEdgeInsets = UIEdgeInsets(top: 7, left: 6, bottom: 0, right: 0)
            v.titleLabel?.font = 12.font_medium
            v.contentHorizontalAlignment = .leading
            v.addTarget(self, action: #selector(searchButtonClick), for: .touchUpInside)
            v.backgroundColor = "#EDEEF0".toColor
            v.layer.cornerRadius = 15.0.Scale
            v.layer.masksToBounds = true
        }
        
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = ["圈子","论坛","话题"]
        
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = "#47494D".toColor
        segmentedDataSource.titleSelectedColor = "#17181A".toColor
        segmentedDataSource.titleNormalFont = 15.font_semiBold!
        segmentedDataSource.titleSelectedFont = 17.font_semiBold!
        segmentedDataSource.itemSpacing = 11.0.Scale
        segmentedView.dataSource = segmentedDataSource
        
        let indicator = JXSegmentedIndicatorImageView()
        indicator.image = "Home_tag".toImage
        indicator.indicatorWidth = 13.0.Scale
        indicator.indicatorHeight = 13.0.Scale
        segmentedView.indicators = [indicator]
        
        segmentedView.delegate = self
        segmentedView.backgroundColor = .clear
        segmentedView.defaultSelectedIndex = 1
        
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        view.addSubview(listContainerView)
        
        segmentedView.listContainer = listContainerView
        
        segmentedView.leading(0).top(CGFloat.statusHeight).height(44.0.Scale).width(130.0.Scale)
        searchButton.trailing(12.0.Scale).leading(165.0.Scale).height(30.0.Scale)
        searchButton.CenterY == segmentedView.CenterY
        
        listContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(segmentedView.snp.bottom)
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(toLuntanListVC), name: .toLuntanListVC, object: nil)
    }
    
    private func loadData(){
        
        self.segmentedDataSource.titles = ["圈子","论坛","话题"]
        self.segmentedView.reloadData()
    }
    
    @objc private func searchButtonClick(){
        let searchVC = ClubForumSearchViewController()
        searchVC.type = index
        self.navigationController?.pushViewController(searchVC, animated: true)
    }

    @objc private func toLuntanListVC() {
        self.segmentedView.selectItemAt(index: 1)
    }
}

//MARK: JXSegmentedListContainerViewDataSource
extension ClubSquareViewController:JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        
        if index == 1 {
            return ClubStrategyListViewController()
        }else if index == 2 {
            return ClubTopicListViewController()
        }
        return ClubDynamicViewController()
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        
        self.index = index
        if index == 2 {
            searchButton.isHidden = true
        }else{
            searchButton.isHidden = false
        }
    }
}
